package com.ohos.carbon.slice.demos;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.DemoDataBean;
import com.ohos.carbon.provider.DemosAdapter;
import com.ohos.carbon.utils.FocusUtil;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;
import ohos.multimodalinput.event.TouchEvent;

import java.util.ArrayList;
import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class CollapsingtoolbarSlice extends AbilitySlice {

    private Image back_button, image1, image2;
    private Text text_title;
    private ListContainer listContainer;
    private ArrayList<DemoDataBean> mList;
    private int a;
    private int b;
    private int h = 150;
    private int p = 30;
    private int w;
    private int focus;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_collapsingtoolbar);
        initData();
        initView();
        initZoom();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Collapsing toolbar")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Collapsing toolbar")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Collapsing toolbar", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Collapsing toolbar", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Collapsing toolbar", 1);
            pf.flushSync();
        }
    }

    private void initData() {
        mList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            mList.add(new DemoDataBean(0, null, null, null, null));
            mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background1, null, "Henry Russell", "Lorem ipsum dolor sit amet", "21:42,04 3月"));
            mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background2, null, "Heather Doris Russell", "Proin nibh augue , suscipit a", "21:54,04 3月"));
            mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background3, null, "Diane Beverly Martinez", "Cras vel lorem", "07:26,04 3月"));
            mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background4, null, "Vuctor Gregory Henderson", "Etiam pellentesque aliquet tellus", "20:34,04 3月"));
        }
    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        image1.setVisibility(Component.HIDE);
        image2.setVisibility(Component.HIDE);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Collapsing toolbar");
        text_title.setMarginTop(155);

        listContainer = (ListContainer) findComponentById(ResourceTable.Id_listview);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        listContainer.setItemProvider(new DemosAdapter(mList, getContext()));
        back_button.setClickedListener(component -> onBackPressed());
    }

    private void initZoom() {
        listContainer.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                switch (touchEvent.getAction()) {
                    case TouchEvent.PRIMARY_POINT_DOWN:
                    case TouchEvent.POINT_MOVE:
                        focus = FocusUtil.getFocus(touchEvent);
                        a = focus;
                        if (b == 0) {
                            b = a;
                        }
                        if (b > focus) {
                            if (p <= b - focus) {
                                w = h = h - 5;
                                if (w >= 0) {
                                    text_title.setMarginTop(w);
                                } else {
                                    h = 0;
                                }
                            }
                        }
                        if (b < focus) {
                            if (p <= focus - b) {
                                if (w <= 150) {
                                    w = h = h + 5;
                                    text_title.setMarginTop(w);
                                }
                            }
                        }
                        break;
                    case TouchEvent.PRIMARY_POINT_UP:
                        b = 0;
                        break;
                }
                return false;
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
